package com.kaifamiao.controller;

import com.kaifamiao.entity.Customer;
import com.kaifamiao.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.time.LocalDate;
import java.util.Map;
import java.util.logging.Logger;

@Controller
@RequestMapping(path = "/customer/**")
public class CustomerController {

    private Logger logger;

    // 根据类型自动装配
    @Autowired
    private CustomerService customerService;

    // 使用构造方法实现自动装配
    public CustomerController( @Autowired Logger logger ) {
        super();
        this.logger = logger;
        logger.info( "创建控制器实例" );
    }

    @RequestMapping(path = "/sign/up/page")
    public String signUpPage() {
        logger.info( "打开注册页面" );
        // prefix: `/WEB-INF/views/`
        String viewName = "customer/sign-up";
        // suffix: `.jsp`
        return viewName; // "forward:/WEB-INF/views/customer/sign-up.jsp"
    }

    @RequestMapping(path = "/sign/in/page")
    public String signInPage() {
        logger.info( "打开登录页面" );
        String viewName = "customer/sign-in";
        return viewName; // "forward:/WEB-INF/views/customer/sign-in.jsp"
    }

    @RequestMapping(path = "/sign/up/first")
    public String signUp1( String email,
                           String tel,
                           String birthdate,
                           String married,
                           String password,
                           String confirm) {
        logger.info( "注册" );
        Customer c = new Customer();
        c.setEmail(email);
        c.setTel(tel);
        c.setPassword(password);
        c.setMarried( "Y".equalsIgnoreCase( married ) );
        c.setBirthdate( LocalDate.parse( birthdate ) );
        customerService.save( c );
        // 重定向到指定位置
        return "redirect:/customer/sign/in/page";
    }

    @RequestMapping(path = "/sign/up/second")
    public String signUp2(Customer c, String confirm, RedirectAttributes attrs) {
        logger.info( "注册" );
        String password = c.getPassword();
        if( password != null && password.equals( confirm ) ) {
            customerService.save(c);
            // 重定向到指定位置
            return "redirect:/customer/sign/in/page";
        }
        // 可以通过 RedirectAttributes 向重定向后的视图传递数据(一次性使用)
        attrs.addFlashAttribute( "message", "两次密码不一致" );
        return "redirect:/customer/sign/up/page";
    }

    @RequestMapping("/sign/in/first")
    public String signIn1(String email, @RequestParam("password") String passwd, Model model) {
        if( email == null || email.isEmpty() ) {
            model.addAttribute( "message", "邮箱不能为空" );
            return "customer/sign-in";
        }
        if( passwd == null || passwd.isEmpty() ) {
            model.addAttribute( "message", "密码不能为空" );
            return "customer/sign-in";
        }
        return "customer/main";
    }

    @RequestMapping("/sign/in/second")
    public String signIn2(String email, @RequestParam("password") String passwd, Map<String,Object> map) {
        if( email == null || email.isEmpty() ) {
            map.put( "message", "邮箱不能为空" );
            return "customer/sign-in";
        }
        if( passwd == null || passwd.isEmpty() ) {
            map.put( "message", "密码不能为空" );
            return "customer/sign-in";
        }
        return "customer/main";
    }

    @RequestMapping("/sign/in/third")
    public ModelAndView signIn3(String email, String password) {
        ModelAndView mav = new ModelAndView();
        if( email == null || email.isEmpty() ) {
            mav.addObject("message", "邮箱不能为空" );
            mav.setViewName( "customer/sign-in" );
            return mav;
        }
        if( password == null || password.isEmpty() ) {
            mav.addObject( "message", "密码不能为空" );
            mav.setViewName( "customer/sign-in" );
            return mav;
        }
        mav.setViewName( "customer/main" );
        return mav;
    }

    @RequestMapping("/sign/in/fourth")
    public String signIn4(String email, String password, ModelMap modelMap ){
        if( email == null || email.isEmpty() ) {
            modelMap.addAttribute( "message", "邮箱不能为空" );
            return "customer/sign-in";
        }
        if( password == null || password.isEmpty() ) {
            modelMap.addAttribute( "message", "密码不能为空" );
            return "customer/sign-in";
        }
        return "customer/main";
    }

    public CustomerService getCustomerService() {
        return customerService;
    }

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }
}
